﻿using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace EmbeddedLanguage.SubProcessors
{
    public class IfJSProcessor : ExpressionToJSProcessor
    {

        //public string KeyVariableName;
        private bool EnteredFirstMethod;

        public override Expression Visit(Expression node)
        {
            return base.Visit(node);
        }

        

        protected override Expression VisitMethodOrDelegateCall(Expression node, Expression node_Object,
            MemberInfo node_Method, ReadOnlyCollection<Expression> node_Arguments, bool FromInvocation)
        {
            if (EnteredFirstMethod)
            {
                return base.VisitMethodOrDelegateCall(node, node_Object, node_Method, node_Arguments, FromInvocation);
            }
            else
            {
                EnteredFirstMethod = true;
                
                if (node_Object == null)
                {
                    var ns = new NestedString
                    {
                        Parent = this.CurrentNestedString,
                        Comment = "IfJSProcessor",
                        Header_Template = "if({IfCondition}){",
                        LHS_Template = "{ThenAction}",
                        Waist = "}else{",
                        //ForceLineBreakOnRHS = true,
                        //ChildrenContinueOnLine = false,
                        RHS_Template = "{ElseAction}",
                        Footer = "}",
                    };
                    this.RemoveReturnFromParent(ns);
                    //ns.IsKaboose = true;
                    ns.ChildSubstitutions = new Dictionary<string, int>
                    {
                        {"{IfCondition}", 1},
                        {"{ThenAction}", 2},
                        {"{ElseAction}", 3},
                        
                    };
                    ns.IgnoredChildren = new List<int>
                    {
                        0
                    };
                    Func<int, ExpressionToJSProcessor> processors = i =>
                    {
                        switch (i)
                        {
                            //case 0:
                            case 1:
                            case 2:
                            case 3:
                                var extractor = new JSFunctionExtractor(this.KeyVariableName);
                                return extractor;
                        };
                        return null;
                    };
                    this.AddArgsToNestedString(ns, node_Arguments, processors);
                }
                else
                {
                    throw new NotImplementedException();
                }
                //this.CurrentNestedString = ns.Parent;
                return node;

            }
        }
    }
}
